package com.gem.Customer.dao.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

import com.gem.Customer.entity.Customer;
import com.gem.Customer.service.Menu;

public class FileOperate {
	static Scanner scanner = new Scanner(System.in);

	public static void main(String[] args) {

		method();

	}

	//调用菜单方法
	public static void method() {
		ArrayList<Customer> list = new ArrayList<Customer>();
		for (;;) {
			Menu.showMenu();
			System.out.println("请选择操作:");
			int nextInt = scanner.nextInt();
			if (nextInt == 1) {
				insert(list);
			} else if (nextInt == 2) {
				delect(list);
			} else if (nextInt == 3) {
				update(list);
			} else if (nextInt == 4) {
				search(list);
			} else if (nextInt == 5) {
				showAll(list);
			} else if (nextInt == 0) {
				Menu.quit();
				break;
			} else {
				System.out.println("请重新选择操作!");
			}
		}
		write(list);
	}

	//读入文件
	public static void write(ArrayList<Customer> list) {
		File file = new File("Info.dat");
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try (PrintWriter pw = new PrintWriter(new FileWriter(file), true);) {
			for (Customer customer : list) {
				pw.write(customer.getId() + ",");
				pw.write(customer.getName() + ",");
				pw.write(customer.getAddress() + "\r\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//读出文件
	public static void begin(ArrayList<Customer> list) {
		File file = new File("Info.dat");
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			BufferedReader br = new BufferedReader(new FileReader("Info.dat"));
			String line = null;
			try {
				while ((line = br.readLine()) != null) {
					String[] strArray = line.split(",");
					Customer cus = new Customer();
					cus.setId(Integer.valueOf(strArray[0]));
					cus.setName(strArray[1]);
					cus.setAddress(strArray[2]);
					list.add(cus);
				}
				br.close();
			} catch (NumberFormatException | IOException e) {
				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	//插入方法
	public static void insert(ArrayList<Customer> list) {
		System.out.println("请输入编号:");
		Integer id = scanner.nextInt();
		boolean b = true;
		for (int index = 0; index < list.size(); index++) {
			if (id.equals(list.get(index).getId())) {
				System.out.println("您输入的客户已存在!");
				b = false;
			}
		}
		if (b) {
			System.out.println("请输入名称:");
			String name = scanner.next();
			System.out.println("请输入地址:");
			String address = scanner.next();
			System.out.println("新增成功!");
			Customer cus = new Customer(id, name, address);
			list.add(cus);
		}
	}

	//删除方法
	public static void delect(ArrayList<Customer> list) {
		System.out.println("请输入要删除的编号:");
		Integer id = scanner.nextInt();
		boolean b = false;
		for (Customer customer : list) {
			begin(list);
			if (id.equals(customer.getId())) {
				System.out.println("删除成功!");
				list.remove(customer);
				b = true;
				break;
			}
		}
		if (!b) {
			System.out.println("删除的信息不存在!");
		}
	}

	//更新方法
	public static void update(ArrayList<Customer> list) {
		begin(list);
		System.out.println("请输入编号:");
		Integer id = scanner.nextInt();
		System.out.println("请输入名称:");
		String name = scanner.next();
		System.out.println("请输入地址:");
		String address = scanner.next();
		Customer cus = new Customer(id, name, address);
		if (!list.isEmpty()) {
			for (int index = 0; index < list.size(); index++) {
				if (id.equals(list.get(index).getId())) {
					list.set(index, cus);
					System.out.println("修改成功!");
				}
			}
		} else {
			System.out.println("没有客户信息,无法修改!");
		}
	}

	//查询方法
	public static void search(ArrayList<Customer> list) {
		begin(list);
		for (;;) {
			Menu.subMenu();
			int id = scanner.nextInt();
			if (id == 1) {
				search1(list);
			}
			if (id == 2) {
				search2(list);
			}
			if (id == 3) {
				search3(list);
			}
			if (id == 4) {
				break;
			}
		}
	}

	//按照编号查询
	public static void search1(ArrayList<Customer> list) {
		System.out.println("请输入编号:");
		Integer id = scanner.nextInt();
		boolean b = false;
		for (int index = 0; index < list.size(); index++) {
			if (id.equals(list.get(index).getId())) {
				System.out.println("\t" + list.get(index).getId() + "\t" + list.get(index).getName() + "\t"
						+ list.get(index).getAddress());
				b = true;
			}
		}
		if (!b) {
			System.out.println("该客户信息不存在!");
		}
	}

	//按照名称查询
	public static void search2(ArrayList<Customer> list) {
		System.out.println("请输入名称:");
		String name = scanner.next();
		boolean b = false;
		for (int index = 0; index < list.size(); index++) {
			if (name.equals(list.get(index).getName())) {
				System.out.println("\t" + list.get(index).getId() + "\t" + list.get(index).getName() + "\t"
						+ list.get(index).getAddress());
				b = true;
			}
		}
		if (!b) {
			System.out.println("您查找的客户不存在!");
		}
	}

	//按照地址查询
	public static void search3(ArrayList<Customer> list) {
		System.out.println("请输入地址:");
		String address = scanner.next();
		boolean b = false;
		for (int index = 0; index < list.size(); index++) {
			if (address.equals(list.get(index).getAddress())) {
				System.out.println("\t" + list.get(index).getId() + "\t" + list.get(index).getName() + "\t"
						+ list.get(index).getAddress());
				b = true;
			}
		}
		if (!b) {
			System.out.println("该客户信息不存在!");
		}
	}

	//浏览方法
	public static void showAll(ArrayList<Customer> list) {
		System.out.println("结果:" + "\t" + "编号" + "\t" + "名称" + "\t" + "地址" + "\t");
		begin(list);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(
					"\t" + list.get(i).getId() + "\t" + list.get(i).getName() + "\t" + list.get(i).getAddress());
		}
	}
}
